/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.indexer.analyzers;

import edu.ucsd.db.bassarisk.indexer.api.IPosting.ISequencedPosting;
import edu.ucsd.db.bassarisk.indexer.api.IPostingFactory;
import edu.ucsd.db.bassarisk.indexer.api.IPostingHandler;
import edu.ucsd.db.bassarisk.indexer.api.IStageOneAnalyzerFactory;

/**
 * A posting analyzer that converts the tokenKey of postings with no groupKey to
 * lower-case. This accomplishes indexing only the lower case normalization of
 * keywords for the time being.
 * 
 * TODO: The way this analyzer chooses which postings to modify is sloppy and
 * wrong. It shows a weakness in the design of the posting class.
 * 
 * @author nbales
 */
public class LowerCaseNormalizerFactory implements IStageOneAnalyzerFactory {

  @Override
  public IPostingHandler<ISequencedPosting>
      createAnalyzer(final IPostingHandler<ISequencedPosting> target,
                     final IPostingFactory factory) {
    return new IPostingHandler<ISequencedPosting>() {
      @Override
      public void post(final ISequencedPosting posting) {
        if (posting.getGroupKey().equals("")) {
          target.post(factory.tokenPosting(posting.getTokenKey().toLowerCase()));
        } else {
          target.post(posting);
        }
      }
    };
  }

}
